package iox

import (
	"io"
	"log"
)

func ReadCloser(r io.Reader) io.ReadCloser {
	if r == nil {
		return nil
	}
	if rc, ok := r.(io.ReadCloser); ok {
		return rc
	}
	return io.NopCloser(r)
}

func WriteCloser(w io.Writer) io.WriteCloser {
	if w == nil {
		return nil
	}
	if wc, ok := w.(io.WriteCloser); ok {
		return wc
	}
	return &nopCloser{w}
}

func Close(closers ...interface{}) {
	for _, closer := range closers {
		if closer != nil {
			if c, ok := closer.(io.Closer); ok {
				if err := c.Close(); err != nil {
					log.Println(err)
				}
			}
		}
	}
}

type nopCloser struct{ io.Writer }

func (nopCloser) Close() error { return nil }
